/**
 * Created by Celebration-Grey on 2017/5/8.
 */

admin.level = {};

admin.level.$table = null;

admin.level.$thead = null;

admin.level.$tbody = null;

admin.level.limit = 20;

admin.level.parame = {
	index : 1,
	limit : admin.level.limit
};

admin.level.urls = {
	getLevel : host + 'admin_getLevels'
};

admin.level.field = {
	'level_id' : '等级',
	'level_point' : '分数',
	'level_title' : '称号'
};

admin.level.init = function(){
	admin.level.$table = $('#user-level');
	admin.level.$thead = admin.level.$table.find('thead');
	admin.level.getLevels();
};

admin.level.getLevels = function(){
	admin.ajax(admin.level.urls.getLevel,admin.level.parame,admin.level.getSuccess,admin.error);
};

admin.level.getSuccess = function(data){
	if(data.status == 200){
        admin.level.parame.index++;
        admin.level.fillHead(data.data[0]);
        admin.level.fillDatas(data.data);
	}
	else{
		$.Huimodalalert(data.message,5000);
	}
};

admin.level.fillHead = function(data){
	var field = admin.level.field;
    var $tr = $('<tr></tr>');
    for (var key in data){
        var $td = $('<th class="'+key+'">'+field[key]+'</th>');
        $tr.append($td);
    }
    $tr.append('<th class="action">操作</th>');
    admin.level.$thead.append($tr);
}

admin.level.fillDatas = function(data){
	var $tbody = $('<tbody></tbody>');
	for(var i = 0; i < data.length; i++){
		$tbody.append(admin.level.fillData(data[i]));
	}
	admin.level.$table.append($tbody);
};

admin.level.fillData = function(data){
	var $tr = $('<tr></tr>');
	for(var key in data){
		var $td = $('<td class="'+key+'" title="'+data[key]+'">'+data[key]+'</td>');
		$tr.append($td);
	}
	var $action = $('<td class="action"><span class="icon Hui-iconfont show">&#xe665;</span><span class="icon Hui-iconfont edit">&#xe60c;</span><span class="icon Hui-iconfont delete">&#xe6e2;</span></td>');
    $tr.append($action);
	return $tr;
};